<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container" @click="updateImg">
        <img src="../../assets/loginImg/logo.png" alt="">
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <i class="iconfont icon-shouji" />
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <i class="iconfont icon-mima" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <i class="iconfont icon-yanzhengyanzhengma" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="code"
          @keyup.enter.native="handleLogin"
        />
        <div class="validateImg" @click="updateImg"> <img :src="codeImg" alt=""></div>
      </el-form-item>
      <el-button class="btnSytle" :loading="loading" type="primary" style="width:100%;margin-bottom:30px; " @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">loginName: admin</span>
        <span> password: admin</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { valiDateImg } from '@/api'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位数字'))
      } else {
        callback()
      }
    }
    return {
      codeImg: '',
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '', // 验证码
        clientToken: '', // 验证码token
        loginType: 0 // 这里是后台管理的登入
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', message: '验证码必填' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    /*  $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    } */
  },
  async created() {
    this.updateImg()
  },
  methods: {
    // 更新 验证码
    // 获取验证码
    async updateImg() {
      // 随机数
      // console.log(1)
      const random = Math.floor(Math.random() * 10)
      this.loginForm.clientToken = random

      const res = await valiDateImg(random)
      // console.log(this.clientToken)
      // console.log(res)
      this.codeImg =
            'data:image/png;base64,' +
            btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))
      // console.log(this.codeImg)
    },

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      // console.log('登录')
      // 兜底校验
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            // 登录
            // const res = await LoginApi(this.loginForm)
            // const res = this.$store.dispatch('user/login', this.loginForm)
            this.$store.dispatch('user/login', this.loginForm) // 直接判断是 正确状态的 promise 还是错误状态的 try
            // console.log(res.then(() => {}))
            // // console.log(111)
            // if (res.data.success) {
            this.$router.push('/')
            this.loading = false
            // } else {
            //   this.$message.error(res.data.msg)
            //   this.loading = false
            // }
          } catch (error) {
            console.log(error)
            this.loading = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

/* @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
} */

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      margin-left: 4px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }

    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0);
    border-radius: 5px;
    border:1px solid #ccc ;
    color: #999;

  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/loginImg/Login_background.png') no-repeat;
  background-size: cover;
  overflow: hidden;
  position: relative;
//   align-items: center;
// display: flex;

  .login-form {
    position: absolute;
    width: 520px;
    max-width: 100%;
    padding: 76px 35px 0;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 10px;
    margin: 0 auto;
    // overflow: hidden;
    background-color: #fff;

  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: inherit;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    i{
      font-size: 20px;
    }
  }

  .title-container {
    position: relative;
    img {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -120px;
      left:50%;
      margin-left:-48px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.validateImg{
    position: absolute;
      // background-color: pink;
      height: 100%;
      width: 33%;
      top:0;
      right: 0;
      img{
        width: 100%;
        height: 100%;
        background-size: cover;
      }
    }
    .btnSytle{
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    }
</style>
